package com.vchello.util;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

/**
 * 线程池
 *
 * @author admin
 *
 */
public class ThreadPool {
	public static Logger log = Logger.getLogger(ThreadPool.class);
	public static ThreadPoolExecutor tpe = null;

	public static ThreadPoolExecutor getInstance() {
		Object obj = null;
		if (tpe == null) {
			obj = "obj";
			synchronized (obj) {
				if (tpe == null) {
					tpe = new ThreadPoolExecutor(10, 80, 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(10000),
							new ThreadPoolExecutor.DiscardOldestPolicy());
				}
			}
		}
		return tpe;
	}

	private static int count = 0;

	public static void toSwf(String filePath) {
		try {
			count++;
			if (count == 100000) {
				// tpe.shutdown();
				Thread.sleep(300);
				count = 0;
			}
			tpe = getInstance();
			// tpe.execute(new ToSwfThread(filePath));
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		} finally {

		}
	}

	public static void main(String[] args) {
		try {
			getInstance();
			// for (int i = 0; i < 1000; i++) {
			// System.out.println(tpe.getPoolSize());
			// System.out.println(tpe.getMaximumPoolSize());
			toSwf("D://swf//3.docx");
			// logLook(58,0,null,"127.0.0.1");
			// }
		} finally {
			tpe.shutdown();
		}

	}

	public static void sendEmail(String[] addresses, String subject, String message, String filePath) {
		try {
			count++;
			if (count == 100000) {
				// tpe.shutdown();
				Thread.sleep(300);
				count = 0;
			}
			tpe = getInstance();
			tpe.execute(new SendEmailThread(addresses, subject, message, filePath));
		} catch (Exception e) {
			// TODO: handle exception
		}
		// TODO Auto-generated method stub
	}

}

class SendEmailThread extends Thread {
	private String[] addresses;
	private String subject;
	private String message;
	private String filePath;

	public SendEmailThread(String[] addresses, String subject, String message, String filePath) {
		this.addresses = addresses;
		this.subject = subject;
		this.message = message;
		this.filePath = filePath;
	}

	public void run() {
		try {
			SendEmail sendEmail = SendEmail.getInstance();
			if ("1".equals(VchelloConfig.getSendEmailStatus())) {
				sendEmail.sendMessage(addresses, subject, message, filePath);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

// class ToSwfThread extends Thread{
// private String filePath;
// public ToSwfThread(String filePath){
// this.filePath = filePath;
// }
// public void run(){
// try {
// DocConverter docConverter = new DocConverter();
// boolean flag = docConverter.conver(filePath);
//// System.out.println(flag);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// ThreadPool.log.error(e);
// }
// }
// }